﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace GerritDrost.VirtualBoxManager.WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        VirtualMachineManager manager;
        private VirtualMachineManager.MachinesUpdatedDelegate updateDelegate;
        System.Windows.Forms.NotifyIcon icon;

        public MainWindow()
        {
            InitializeComponent();

            manager = new VirtualMachineManager(@"C:\Program Files\Oracle\VirtualBox", 5000);

            updateDelegate = new VirtualMachineManager.MachinesUpdatedDelegate(updateVirtualMachines);

            manager.MachinesUpdated += updateVirtualMachines;

            icon = new System.Windows.Forms.NotifyIcon();
            icon.Icon = new System.Drawing.Icon("Resources/VirtualBox.ico");
            icon.Click += icon_Click;
            icon.Visible = true;
        }

        void icon_Click(object sender, EventArgs e)
        {
            if (this.IsVisible)
            {
                this.Hide();
            }
            else
            {
                this.Show();
                this.Activate();
            }
        }


        private void updateVirtualMachines()
        {
            if (!this.Dispatcher.CheckAccess())
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, updateDelegate);
                return;
            }

            string selectedUuid = "NULL";

            if (selectedMachine != null)
                selectedUuid = selectedMachine.Uuid;

            machineListView.Items.Clear();

            List<VirtualMachine> machines = manager.VirtualMachines;
            
            foreach (VirtualMachine machine in machines)
            {
                machineListView.Items.Add(machine);

                if (machine.Uuid == selectedUuid)
                    machineListView.SelectedItem = machine;
            }

            updateSelection();
        }

        private VirtualMachine selectedMachine;

        private void machineListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            updateSelection();
        }

        private void updateSelection()
        {
            selectedMachine = (VirtualMachine)machineListView.SelectedItem;

            if (selectedMachine == null)
                return;

            resumeButton.IsEnabled = !selectedMachine.Running;
            suspendButton.IsEnabled = selectedMachine.Running;
        }

        private void suspendButton_Click(object sender, RoutedEventArgs e)
        {
            manager.SuspendVirtualMachine(selectedMachine.Uuid);
        }

        private void resumeButton_Click(object sender, RoutedEventArgs e)
        {
            if (selectedMachine == null)
                return;

            manager.ResumeVirtualMachine(selectedMachine.Uuid);
        }

        private bool polling = false;
        private void Window_Activated(object sender, EventArgs e)
        {
            if (!polling)
                manager.StartPolling();
        }

        private void Window_Deactivated(object sender, EventArgs e)
        {
            if (polling)
                manager.StopPolling();
        }

        private void Window_MouseEnter(object sender, MouseEventArgs e)
        {
            if (!polling)
                manager.StartPolling();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            icon.Visible = false;
            icon.Dispose();
        }
    }
}
